<?php


namespace app\common\model\mysql;


use think\facade\Request;

class Goods extends BaseModel
{
    public function searchTitleAttr($query, $value){
        $query->where('title', 'like', '%' . $value . '%');
    }
    public function searchCreateTimeAttr($query, $value){
        $query->whereBetweenTime('create_time', $value[0], $value[1]);
    }
    /**
     * 获取列表数据
     * @param $data
     * @param int $num
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getLists($likeKeys, $data, $num = 10){
        $order = ["listorder" => "desc", "id" => "desc"];
        // $list = $this->where("status", "<>", config("status.mysql.table_delete"))
        if(!empty($likeKeys)){
            // 搜索器
            $res = $this->withSearch($likeKeys, $data);
        } else {
            $res = $this;
        }
        $list = $res->whereIn("status", [0,1])
            ->order($order)
            ->paginate($num);
        // echo $this->getLastSql();exit;
        return $list;
    }

    public function getNormalGoodsByCondition($where, $field = true, $limit = 5){
        $order = ['listorder' => 'desc', 'id' => 'desc'];
        $where['status'] = config('status.success');
        $result = $this->where($where)
            ->order($order)
            ->field($field)
            ->limit($limit)
            ->select();
        return $result;
    }

    // 获取器 单个图片地址处理
    public function getImageAttr($value){
        // 1 vue代码与服务器代码不在一台服务器，存图片不存域名
        // 2.PHP图片上传到本地服务器，服务器如果是集群的话图片不可以存储到本地，需要借助第三方云存储（分布式的话容易传到第一台，获取会去第二台找的情况）
        //return request()->domain().$value;
        return input('server.REQUEST_SCHEME') . '://' . request()->server('HTTP_HOST').$value;
    }
    // 获取器 多个逗号分隔的图片地址处理
    public function getCarouselImageAttr($value){
        if(!empty($value)){
            $value = explode(",", $value);
            $value = array_map(function($v){
                return input('server.REQUEST_SCHEME') . '://' . request()->server('HTTP_HOST').$v;
            }, $value);
        }
        return $value;
    }

    public function getNormalGoodsFindInSetCategoryId($categoryId, $field = true, $limit = 10){
        $order = ['listorder' => 'desc', 'id' => 'desc'];
        $result = $this->whereFindInSet("category_path_id", $categoryId)
            ->where("status", "=", config('status.success'))
            ->order($order)
            ->field($field)
            ->limit($limit)
            ->select();
        //dump($this->getLastSql());die;
        return $result;
    }

    public function getNormalLists($data, $num, $field, $order){
        // 这张表缺少listorder字段  goods表
        //$order = ['listorder' => 'desc', 'id' => 'desc'];
        $res = $this;
        if(isset($data['category_path_id'])){
            $res = $this->whereFindInSet("category_path_id", $data["category_path_id"]);
        }
        $list = $res->where("status", "=", config("status.success"))
            ->order($order)
            ->field($field)
            ->paginate($num);
        //echo $this->getLastSql();exit;
        return $list;
    }
}